Application.ExecuteCommand("Quick Filter Components")

dim ws : set ws = CreateObject("WScript.Shell")
ws.RegWrite "HKEY_CURRENT_USER\Software\MyApp\PADS\ApplicationMode", 0, "REG_EXPAND_SZ"
ws.RegWrite "HKEY_CURRENT_USER\Software\MyApp\PADS\IsUnion", 0, "REG_EXPAND_SZ"
set ws = nothing

if args="" then args = 20'6.063
dim usr_dx
usr_dx = cdbl(trim(args))
dim app,doc,v
set app=application
set doc=app.activedocument
set v = doc.ActiveView
main
msgbox "done"
sub main
	if usr_dx =0 then usr_dx = 20
	doc.Unit = 2'mil
	dim cmps,cmp
	set cmps = doc.getobjects(1,,true)
	'if cmps.count<2 then exit sub
	
	'Get Selected Component Infomation ( poxition X, POSITION Y, NAME, XSIZE,YSIZE)	
	dim i,j
	dim AYarray
	redim AYarray(5,cmps.count)
	dim x,y
	dim x0,y0,x1,y1
	dim x_len,y_len
	dim x_len2,y_len2
	i=0
	dim compobj_r
	for each cmp in cmps
		doc.SelectObjects ppcbObjectTypeAll, "*", False 
		compobj_r = doc.Components(cmp.name).Orientation	
		doc.Components(cmp.name).Selected = True
		v.SetExtentsToSelection 
		x0 = cdbl(v.TopLeftX)
		x1 = cdbl(v.BottomRightX)
		y0 =cdbl( v.BottomRightY)
		y1 = cdbl(v.TopLeftY)
		x_len = abs(x1-x0) -12
		y_len = abs(y1-y0) -4
		
		Application.ExecuteCommand("Rotate 90")
		v.SetExtentsToSelection 
		x0 = cdbl(v.TopLeftX)
		x1 = cdbl(v.BottomRightX)
		y0 =cdbl( v.BottomRightY)
		y1 = cdbl(v.TopLeftY)
		x_len2 = abs(x1-x0) -12
		y_len2 = abs(y1-y0) -4
		if y_len2<x_len then x_len = y_len2
		doc.Components(cmp.name).Orientation =compobj_r
		
		'msgbox cmp.name & chr(9) & x_len & "," & y_len		
		AYarray(0,i) = cmp.positionX
		AYarray(1,i) = cmp.positiony
		AYarray(2,i) = cmp.name
		AYarray(3,i) = x_len
		AYarray(4,i) = y_len	
		i = i+1
	next
	'BUBBLE REORDER
	' for i=0 to cmps.count-1
		' msgbox AYarray(2, i)
	' next
	
	dim tmpx,tmpy,tmpname,tmpbodyx,tmpbodyy
	for i=0 to cmps.count-2
		for j= i+1 to cmps.count-1
			'msgbox AYarray(0, i) & chr(9) & AYarray(0, j)
			If AYarray(0, i) > AYarray(0, j) Then
				'msgbox "bigger"
				tmpx = AYarray(0, i)
				tmpy = AYarray(1, i)
				tmpname = AYarray(2, i)
				tmpbodyx = AYarray(3, i)
				tmpbodyy = AYarray(4, i)

				AYarray(0, i) = AYarray(0, j)
				AYarray(1, i) = AYarray(1, j)
				AYarray(2, i) = AYarray(2, j)
				AYarray(3, i) = AYarray(3, j)
				AYarray(4, i) = AYarray(4, j)

				AYarray(0, j) = tmpx
				AYarray(1, j) = tmpy
				AYarray(2, j) = tmpname
				AYarray(3, j) = tmpbodyx
				AYarray(4, j) = tmpbodyy
			else
				'msgbox "small"
			End If
		next
		'msgbox AYarray(2, i)
	next
	'for i=0 to cmps.count-1
		'msgbox AYarray(2, i)
	'next
	dim y_min,x_center,y_start
	y_min = AYarray(1,0)
	x_center = AYarray(0,0)
	y_start = AYarray(1,0)' + 0.5 * AYarray(3,0)' +usr_dx
	'doc.SelectObjects ppcbObjectTypeAll, "*", False
	dim nx,selref
	dim dx
	dim compobj
	'msgbox usr_dx
	doc.SelectObjects ppcbObjectTypeAll, "*", False
	for each cmp in cmps
		doc.Components(cmp.name).Selected = True
	next
	v.SetExtentsToSelection 
	
	for i=1 to cmps.count-1
		doc.SelectObjects ppcbObjectTypeAll, "*", False
		selref = AYarray(2,i)
		if selref="" then
		else				
			x_len = AYarray(3,i)
			y_len = 0.5*(AYarray(4,i-1)+AYarray(4,i))
			'msgbox i & chr(9) & selref & "," & x_len
			dx =y_len' 0.5 * cdbl(x_len)
			nx = y_start + dx +usr_dx
			'msgbox nx
			'doc.Components(selref).Selected = True				
			set compobj = doc.Components(selref)
			'msgbox i  & chr(9) & selref & chr(9) & compobj.name
			compobj.move(x_center,nx)	
			'msgbox selref & "-move"
			y_start = nx
		end if
	next
	
	doc.SelectObjects ppcbObjectTypeAll, "*", False
	for each cmp in cmps
		doc.Components(cmp.name).Selected = True
	next
	v.SetExtentsToSelection 
end sub
